package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.GoodsDao;
import org.example.Dao.OrdersDao;
import org.example.Dao.OrdersViewDao;
import org.example.Pojo.Goods;
import org.example.Pojo.Orders;
import org.example.Pojo.OrdersView;
import org.example.Service.OrdersService;
import org.example.Utils.UnionResult;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Resource
    private OrdersDao ordersDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private OrdersViewDao ordersViewDao;


    @Override
    public UnionResult addOrder(Orders orders) {

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",orders.getGoodsid());
        Goods goods = goodsDao.selectOne(queryWrapper);
        double price = goods.getPrice();
        orders.setPrices(price * orders.getCounts());
        orders.setTimes(new Date());
        if (ordersDao.insert(orders) > 0) {
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult QueryMyOrder(Orders orders) {
        QueryWrapper<OrdersView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",orders.getUserid());
        return UnionResult.Success("",ordersViewDao.selectList(queryWrapper));
    }

    @Override
    public UnionResult QueryAllOrder() {
        QueryWrapper<OrdersView> queryWrapper = new QueryWrapper<>();
        return UnionResult.Success("",ordersViewDao.selectList(queryWrapper));
    }

    @Override
    public UnionResult SendOrder(Orders orders) {
        QueryWrapper<OrdersView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",orders.getUid());
        OrdersView ordersView = ordersViewDao.selectOne(queryWrapper);
        orders.setPrices(ordersView.getPrices());
        orders.setOrderstatus(1);
        if (ordersDao.updateById(orders) > 0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }
}
